#!/bin/bash
# rabbit-fence      RabbitMQ fence
#
# chkconfig: 2345 24 79
# description: Starts/Stops RabbitMQ fence daemon
#
# processname: rabbit-fence.py

# Source function library.
. /etc/rc.d/init.d/functions

prog="rabbit-fence"
script="/usr/bin/${prog}.py"
piddir="/var/run/rabbitmq"
pidfile="${piddir}/${prog}.pid"
user="rabbitmq"

[ -x $script ] || exit 0
[ -d $piddir ] || (mkdir -p $piddir; chown -R $user $piddir)

start() {
    exec $script
    return $?
}

stop() {
    PID=$(cat $pidfile)
    kill $PID
    retval=$?
    [ $retval -eq 0 ] && (rm -f $pidfile || true)
    return $retval
}

rh_status() {
    status -p $pidfile $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

restart() {
    stop
    start
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    status)
        rh_status
        ;;
*)
        echo Usage: start|stop|restart|status
        exit 2
esac
